﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace PhotoGallery.Infrastructure
{
    public class RequiredAlbumID : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            var albumID = filterContext.ActionParameters.Where(x => x.Key == "albumID").FirstOrDefault().Value;
            if (albumID == null)
            {
                if (filterContext.Controller.ControllerContext.HttpContext.User.Identity.IsAuthenticated)
                {                
                    filterContext.Result = new RedirectResult("~/Albums");
                }
                else
                {
                    filterContext.Result = new RedirectResult("~/Galleries");
                }
            }
        }
    }
}